package com.shangyitong.yygh_hosp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangyitong.ResultCodeEnum;
import com.shangyitong.client.DictFeignClient;
import com.shangyitong.exception.HospitalException;
import com.shangyitong.yygh.model.hosp.Hospital;
import com.shangyitong.yygh.vo.hosp.HospitalQueryVo;
import com.shangyitong.yygh_hosp.mapper.HospitalRearMapper;
import com.shangyitong.yygh_hosp.repository.HospitalRepository;
import com.shangyitong.yygh_hosp.service.HospitalRearService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class HospitalRearServiceImpl extends ServiceImpl<HospitalRearMapper, Hospital> implements HospitalRearService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public Page<Hospital> showHospitalList(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable = PageRequest.of(page - 1, limit);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        //创建匹配器,即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> allPages = hospitalRepository.findAll(example, pageable);
        allPages.getContent().forEach(this::extractedHospital);
        return allPages;
    }

    @Override
    public Hospital extractedHospital(Hospital item) {
        String hostypeName = dictFeignClient.getName("hostype", item.getHostype());
        String provinceName = dictFeignClient.getSingleName(item.getProvinceCode());
        String cityName = dictFeignClient.getSingleName(item.getCityCode());
        String districtName = dictFeignClient.getSingleName(item.getDistrictCode());
        item.getParam().put("hostypeString", hostypeName);
        item.getParam().put("fullAddress", provinceName + cityName + districtName);
        return item;
    }

    @Override
    public Map<String, Object> showHospDetail(String id) {
        Hospital item = hospitalRepository.getById(id);
        if (item == null){
            throw new HospitalException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        Hospital hospital = extractedHospital(item);
        Map<String, Object> mapHospital = new HashMap<>();
        mapHospital.put("hospital", hospital);
        mapHospital.put("bookingRule", hospital.getBookingRule());
        return mapHospital;
    }

    @Override
    public void updateHospStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.getById(id);
        if (hospital == null){
            throw new HospitalException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        hospital.setStatus(status);
        hospitalRepository.save(hospital);
    }
}